package modell;

import java.io.FileNotFoundException;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Shape3D;

import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;

/**
 * Loads *.obj files.
 */
public class ObjLoader {
	static BranchGroup load(String filename) {
	    int flags = ObjectFile.RESIZE |
	    	ObjectFile.TRIANGULATE | 
	    	ObjectFile.STRIPIFY;
	    double creaseAngle = 60.0;
	    
	    ObjectFile f = new ObjectFile(flags,
	        (float) (creaseAngle * Math.PI / 180.0));
	    Scene s = null;
	    try {
	      s = f.load(filename);
	    } catch (FileNotFoundException e) {
	      System.err.println(e);
	      System.exit(1);
	    } catch (ParsingErrorException e) {
	      System.err.println(e);
	      System.exit(1);
	    } catch (IncorrectFormatException e) {
	      System.err.println(e);
	      System.exit(1);
	    }
	    return s.getSceneGroup();
	}
	
	static Shape3D getShape3D(BranchGroup bg) {
		return (Shape3D) bg.getChild( 0 );
	}
}
